<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Insert title here</title>
        <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/css/style.css" />
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/jquery-easyui-1.4.3/themes/default/easyui.css" />
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/jquery-easyui-1.4.3/themes/icon.css" />
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/css/easyui.icon.css" />
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/jquery-easyui-1.4.3/jquery.min.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/jquery-easyui-1.4.3/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/jquery-easyui-1.4.3/locale/easyui-lang-zh_CN.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/jquery.js/jquery.json.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/jquery.js/jquery.fw.ext.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/js/jquery.form.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/easyui.js/easyui.ext.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/js/message.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/js/MD5.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/js/moment.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/js/zh-cn.js"></script>
    </head>
    <body>
        <div class="easyui-layout" data-options="fit:true">
            <div data-options="region:'north',split:true,border:false" style="height:80px;">
                <form id="searchForm">
                    <table>
                        <tr>
                            <td><a id="btn_add" class="easyui-linkbutton" data-options="group:'g1',iconCls:'icon-add'">添加</a></td>
                            <td><a id="btn_dele" class="easyui-linkbutton" data-options="group:'g1',iconCls:'icon-remove'">删除</a></td>
                            <td><a onclick="grid.datagrid('reload');" href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-arrow_refresh'">刷新</a></td>
                        </tr>
                    </table>
                    <table>
                        <tr>
                            <td>用户名称</td>
                            <td><input name="userNameSch" class="textbox" style="width: 120px; height: 22px;" /></td>
                            <td>创建时间</td>
                            <td><input name="createdStartSch" class="easyui-datebox" style="width: 100px" />
                                -
                                <input name="createdEndSch" class="easyui-datebox" style="width: 100px" />
                            </td>
                            <td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-zoom',plain:true" onclick="grid.datagrid('load',fw.serializeObject($('#searchForm')));">过滤</a> <a href="javascript:void(0);" id="reset_searchFilter" class="easyui-linkbutton" data-options="iconCls:'ext-icon-zoom_out',plain:true" onclick="$('#searchForm input').val('');grid.datagrid('load',{});">重置过滤</a></td>
                        </tr>
                    </table>
                </form>
            </div>
            <div data-options="region:'center',border:false">
                <table id="grid">
                </table>
            </div>
            <div data-options="region:'east',split:true,border:false" style="width: 50%; padding: 5px;">
                <form id="form" name="form" method="post" class="form">
                    <fieldset>
                        <legend> 基本信息--<span id="operateMode_title">添加模式</span> </legend>
                        <table class="table">
                            <tbody>
                                <tr>
                                    <th>ID</th>
                                    <td><input id="id" value="0" name="id" class="textbox" readonly  style="height: 22px;background:#eee;width:100px;" /></td>
                                </tr>
                                <tr>
                                    <th>用户名</th>
                                    <td><input id="userName" name="userName" class="textbox easyui-validatebox" required="true"  style="height: 22px;" /></td>
                                </tr>
                                <tr>
                                    <th>密码</th>
                                    <td><input id="password" name="password" type="password" class="textbox easyui-validatebox" required="true"   style="height: 22px;" /></td>
                                </tr>
                                <tr>
                                    <th>真实姓名</th>
                                    <td><input id="trueName" name="trueName" class="textbox  easyui-validatebox" required="true"  style="height: 22px;" /></td>
                                </tr>
                                 <tr>
					                <th>出生日期</th>
					                <td>
					                	<input id="birthday" name="birthday" class="textbox easyui-validatebox" required="true"    style="height: 22px;" />
					                </td>
					            </tr>
                                <tr>
                                    <th>性别</th>
                                    <td><input id="sex" value="1" name="sex" class="textbox easyui-validatebox"  style="height: 22px;" /></td>
                                </tr>
                                <tr>
                                    <th>手机号</th>
                                    <td><input id="mobilePhone" name="mobilePhone" class="textbox easyui-numberbox" required="true" data-options="validType:'mobile'"   style="height: 22px;" /></td>
                                </tr>
                                 <tr>
                                    <th>地址</th>
                                    <td><input id="address" name="address" class="textbox easyui-validatebox" required="true"    style="height: 22px;" /></td>
                                </tr>
                                <tr>
                                    <th>邮箱</th>
                                    <td><input id="email" name="email" class="textbox easyui-validatebox" required="true" data-options="validType:'email'"   style="height: 22px;" /></td>
                                </tr>
                                <tr>
                                	 <th>添加时间</th>
                                     <td>
                                        <input name="createdTime" class="easyui-datetimebox" style="width: 180px;" />
                                     </td>
                                </tr>
                                <tr>
                                    <th>序号</th>
                                    <td><input id="sortId" name="sortId"
                                            class="easyui-validatebox easyui-numberspinner"
                                            data-options="min:0,max:100,required:true" value="0"   style="width: 50px;" />
                                    </td>
                                </tr> 
                                <tr>    
                                    <th>操作</th>
                                	<td><a id="operateMode_submit" class="easyui-linkbutton" data-options="iconCls:'icon-save'">确定</a> <a id="operateMode_reset" class="easyui-linkbutton" data-options="iconCls:'icon-reload'">重置</a></td>
                                </tr>
                            </tbody>
                        </table>
                    </fieldset>
                </form>
            </div>
        </div>
        <script type="text/javascript">
            $(function() {
                var gird;
                grid=$('#grid').datagrid({
                    fit:true,
                    border : false,
                    collapsible : false,
                    rownumbers : true,
                    pagination : true,
                    singleSelect : true,
                    rownumbers : true,
                    url:'${pageContext.request.contextPath }/user/listUser.do',
                    columns:[[
            		                       
            			{ sortable:true,field:'id',title:'ID'}
            		                       
            			,{ sortable:true,field:'userName',title:'用户名'}
            			
            			,{ sortable:true,field:'trueName',title:'真实姓名'}
            			
            			,{ sortable:true,field:'sex',title:'性别',
            				formatter:function(value,row,index){
		            			if(row.sex == 1){
		            				return "男" 
		            			}else{
		            				return "女"
		            			}
	            			} 
            			}
            			
            			,{ sortable:true,field:'mobilePhone',title:'电话'}
            		                                   		                       
            			,{ sortable:true,field:'sortId',title:'序号'}
            		                       
            			,{ sortable:true,field:'createdTime',title:'创建时间',
            				formatter: function(value, row, index){
            					if(value){
            						return value.substring(0,16);
            					}
    	            			
    	            		 }		
            			}
            		                       
            		                       
            			
            		                    ]],
                    onLoadSuccess : function(data) {
                        $('.iconImg').attr('src', fw.pixel_0);
                        
                    },
                    onClickRow : function(index, row) {
                    	$("#form").form("reset");
                        $("#form").form("load", row);
                        $("#operateMode_title").html("修改模式");
                        
                    }
                });
                //初始化性别
                $('#sex').combobox({
                    valueField: 'id',
                    textField: 'name',
            		panelHeight:'auto',  
            		data:[{id:"0",name:"女"},{id:"1",name:"男"}]
                });
                
                //添加按钮事件
                $("#btn_add").click(function() {
                    $("#operateMode_title").html("添加模式");
                    $('#grid').datagrid("unselectAll");
                    $("#id").attr("value", 0);
                    $("#form").form("reset");
                })
            
                //删除按钮事件
                $("#btn_dele").click(function() {
                    var rows = $('#grid').datagrid('getSelections');
                    if (rows <= 0) {
                        $.messager.show({
                            title : message.title.normal,
                            msg : message.grid_select,
                            timeout : message.timeout,
                            showType : message.showType
                        });
                    } else {
                        $.messager.confirm(message.title.askTitle, message.dele_comfirm, function(r) {
                            if (r) {
                                var idValue = $("#form input[name='id']").val();
                                $.post("${pageContext.request.contextPath }/user/delUser.do", {
                                    id : idValue
                                }, function(result) {
                                    if (result.success) {
                                        $.messager.show({
                                            title : message.title.normal,
                                            msg : message.dele_success,
                                            timeout : message.timeout,
                                            showType : message.showType
                                        });
                                        grid.datagrid('reload');
                                    } else {
                                        $.messager.show({
                                            title : message.title.normal,
                                            msg : message.dele_fail,
                                            timeout : message.timeout,
                                            showType : message.showType
                                        });
                                    }
                                }, 'json');
                                $("#btn_add").click();
                            }
                        });
                    }
                })
            
                //确定按钮事件
                $("#operateMode_submit").click(function() {
                    var idValue = $("#form input[name='id']").val();
                    if ($('#form').form('validate')) {
                        if (idValue > 0) {
                        	$('#password').val(faultylabs.MD5($.trim($('#password').val())));
                        	//修改
                            $.post("${pageContext.request.contextPath }/user/updateUser.do", fw.serializeObject($('form')), function(result) {
                                if (result.success) {
                                    $.messager.show({
                                        title : message.title.normal,
                                        msg : message.update_success,
                                        timeout : message.timeout,
                                        showType : message.showType
                                    });
            
                                    grid.datagrid("reload");
            
                                  //重新加载信息
                                    $("#form").form("load", "${pageContext.request.contextPath }/user/getUserById.do?id=" + idValue);
                                }
                            }, 'json');
                        } else {
                            if(idValue.trim().length == 0){
                                $.messager.show({
                                    title : message.title.normal,
                                    msg : message.add_button_click,
                                    timeout : message.timeout,
                                    showType : message.showType
                                });
                            }else{
                            	
                            	//新增
                            	var rname=$("#userName").val();
				                $.getJSON("${pageContext.request.contextPath }/user/listAllUser.do",{userNameSch:rname,},function(resultUser){
				                	if(resultUser.rows.length==0){
				                		$('#password').val(faultylabs.MD5($.trim($('#password').val())));
				                		
				                		$.post("${pageContext.request.contextPath }/user/addUser.do", fw.serializeObject($('form')), function(result) {
		                                    if (result.success) {
		                                        $.messager.show({
		                                            title : message.title.normal,
		                                            msg : message.add_success,
		                                            timeout : message.timeout,
		                                            showType : message.showType
		                                        });
		                                        grid.datagrid("reload");
		                                        $("#form").form("reset");
		                                    }
		                                }, 'json');
				                	}else{
				                		$.messager.show({
				                            title : message.title.normal,
				                            msg : message.userName_has,
				                            timeout : message.timeout,
				                            showType : message.showType
				                        });

				                	}
				                });
                                
                            }
                        }
                    }
                })
                //重置按钮操作
                $("#operateMode_reset").click(function() {
                    var idValue = $("#form input[name='id']").val();
                    if (idValue > 0) {
                        $("#form").form("load","${pageContext.request.contextPath }/user/getUserById.do?id=" + idValue);
                    } else {
                        $("#form").form("reset");
                    }
                })
            });
        </script>
    </body>
</html>